package com.lysj.bhp.borrowing.job;

import com.lysj.bhp.approve.domain.ApproveRecord;
import com.lysj.bhp.borrowing.base.BorrowingContent;
import com.lysj.bhp.borrowing.domain.BorrowingInstallment;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.sms.MoYunSMSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Component
public class NotifyingUserJob extends BorrowingContent {

    @Scheduled(cron = "00 00 10 * * ?")
   public void notifyingUserJob() throws InterruptedException {
        Date nowDayEndDate = FormatUtil.getDayEndDay(new Date());
        List<BorrowingInstallment> borrowingInstallmentList = borrowingInstallmentRepository.notifyingTheUsers(nowDayEndDate);
        noticeOfPayment( borrowingInstallmentList);
    }
    /**
     * @author zwj
     * @date 2018-09-17 09:23
     * @@Description:通知用户方法
     */
    public void noticeOfPayment(List<BorrowingInstallment> borrowingInstallmentList) throws InterruptedException {
        log.info("发送短信进入");
        List<BorrowingInstallment> borrowingInstallments = new ArrayList<>();
        Long userId = 0l;
        for (BorrowingInstallment borrowingInstallment : borrowingInstallmentList) {
            if(userId.equals(borrowingInstallment.getUserId())){
                borrowingInstallments.add(borrowingInstallment);
            }
            else{
                Thread.sleep(500);
                sendMsg(borrowingInstallment);
            }
            userId = borrowingInstallment.getUserId();
        }
        if(borrowingInstallments.size()>0){
            Thread.sleep(60000);
            noticeOfPayment( borrowingInstallments);
        }
    }
    /**
     * @author zwj
     * @date 2018-09-17 09:11
     * @@Description:发送对应短信方法
     */
    public void sendMsg(BorrowingInstallment borrowingInstallment){
        BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(borrowingInstallment.getBorrowingRecordId());
        String borrowType = "BTC";
        if (borrowingRecord.getBorrowingType() == 20) {
            borrowType = "ETH";
        }
        if (borrowingRecord.getBorrowingType() == 1000) {
            borrowType = "BHPC";
        }
        //短信内容
        String message = "【BHP】尊敬的BHP用户：您好，您的的账户于" + FormatUtil.dateToString(borrowingInstallment.getBeginRepaymentDate()) + "需还款" +
                (borrowingInstallment.getReturnMoney().add(borrowingInstallment.getReturnDividend())).setScale(5, BigDecimal.ROUND_UP) + borrowType + "，请您提前做好资金安排，祝您生活愉快！";
        //获取用户手机号码
        User user = userRepository.findByUserId(borrowingInstallment.getUserId());
        if(!"86".equals(user.getAreaCode())){
            message= "【BHP】Dear BHP user: Your credit loan in BHP account need make payment "+ (borrowingInstallment.getReturnMoney().add(borrowingInstallment.getReturnDividend())).setScale(5, BigDecimal.ROUND_UP)+borrowType+ " before "+ FormatUtil.dateToString2(new Date()) +", please arrange your account balance early. Hope you have a wonderful day!";
        }
        MoYunSMSUtil.sendNotice(user.getPhone(), user.getAreaCode(), message);
    }
}
